package com.imis.module.api.bus;

import com.imis.base.constant.DuplicateCheckConstant;
import com.imis.base.constant.enums.SysTipEnum;
import com.imis.base.globle.Result;
import com.imis.base.util.ConvertUtils;
import com.imis.module.base.BaseBus;
import com.imis.module.system.model.ro.DuplicateCheckRO;
import com.imis.module.system.service.ISysFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * DuplicateCheckBus<br>
 * 数据重复校验 业务处理类
 * </p>
 *
 * @author XinLau
 * @version 1.0
 * @since 2020年06月30日 09:07
 */
@Slf4j
@Service
public class DuplicateCheckBus extends BaseBus {

    /**
     * 文件存放 服务类
     */
    private ISysFileService iSysFileService;

    @Autowired
    public void setSysFileService(ISysFileService iSysFileService) {
        this.iSysFileService = iSysFileService;
    }

    /**
     * 数据重复校验
     *
     * @param duplicateCheck - 重复校验数据模型
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/30 12:01
     */
    public Result doDuplicateCheck(final DuplicateCheckRO duplicateCheck) {
        try {
            Boolean aBoolean = iSysFileService.doDuplicateCheck(duplicateCheck);
            if (aBoolean){
                return Result.errors(SysTipEnum.SC_ERROR_DUPLICATE_KEY);
            }
            return Result.ok();
        } catch (Exception e) {
            return Result.errors(SysTipEnum.SC_ERROR_DUPLICATE_KEY);
        }
    }

    /**
     * 登录账号重复校验
     *
     * @param userName - 登录账号
     * @param id - 主键
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/30 12:01
     */
    public Result checkUserName(final String userName, final Long id) {
        DuplicateCheckRO duplicateCheck = new DuplicateCheckRO();
        duplicateCheck.setTableName(DuplicateCheckConstant.TABLE_NAME_SYS_USER);
        duplicateCheck.setFieldName(DuplicateCheckConstant.FIELD_NAME_USER_NAME);
        duplicateCheck.setFieldValue(userName);
        if (ConvertUtils.isNotEmpty(id)){
            duplicateCheck.setDataId(id);
        }
        return doDuplicateCheck(duplicateCheck);
    }

    /**
     * 手机号重复校验
     *
     * @param phone - 手机号
     * @param id - 主键
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/30 12:01
     */
    public Result checkUserPhone(final String phone, final Long id) {
        DuplicateCheckRO duplicateCheck = new DuplicateCheckRO();
        duplicateCheck.setTableName(DuplicateCheckConstant.TABLE_NAME_SYS_USER);
        duplicateCheck.setFieldName(DuplicateCheckConstant.FIELD_NAME_USER_PHONE);
        duplicateCheck.setFieldValue(phone);
        if (ConvertUtils.isNotEmpty(id)){
            duplicateCheck.setDataId(id);
        }
        return doDuplicateCheck(duplicateCheck);
    }

    /**
     * 邮箱重复校验
     *
     * @param email - 邮箱
     * @param id - 主键
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/30 12:01
     */
    public Result checkUserEmail(final String email, final Long id) {
        DuplicateCheckRO duplicateCheck = new DuplicateCheckRO();
        duplicateCheck.setTableName(DuplicateCheckConstant.TABLE_NAME_SYS_USER);
        duplicateCheck.setFieldName(DuplicateCheckConstant.FIELD_NAME_USER_EMAIL);
        duplicateCheck.setFieldValue(email);
        if (ConvertUtils.isNotEmpty(id)){
            duplicateCheck.setDataId(id);
        }
        return doDuplicateCheck(duplicateCheck);
    }

    /**
     * 角色名称重复校验
     *
     * @param roleName - 角色名称
     * @param id - 主键
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/30 12:01
     */
    public Result checkRoleName(final String roleName, final Long id) {
        DuplicateCheckRO duplicateCheck = new DuplicateCheckRO();
        duplicateCheck.setTableName(DuplicateCheckConstant.TABLE_NAME_SYS_ROLE);
        duplicateCheck.setFieldName(DuplicateCheckConstant.FIELD_NAME_ROLE_NAME);
        duplicateCheck.setFieldValue(roleName);
        if (ConvertUtils.isNotEmpty(id)){
            duplicateCheck.setDataId(id);
        }
        return doDuplicateCheck(duplicateCheck);
    }

    /**
     * 角色编码重复校验
     *
     * @param roleCode - 角色编码
     * @param id - 主键
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/30 12:01
     */
    public Result checkRoleCode(final String roleCode, final Long id) {
        DuplicateCheckRO duplicateCheck = new DuplicateCheckRO();
        duplicateCheck.setTableName(DuplicateCheckConstant.TABLE_NAME_SYS_ROLE);
        duplicateCheck.setFieldName(DuplicateCheckConstant.FIELD_NAME_ROLE_CODE);
        duplicateCheck.setFieldValue(roleCode);
        if (ConvertUtils.isNotEmpty(id)){
            duplicateCheck.setDataId(id);
        }
        return doDuplicateCheck(duplicateCheck);
    }

    /**
     * 功能菜单名称重复校验
     *
     * @param menuName - 功能菜单名称
     * @param id - 主键
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/30 12:01
     */
    public Result checkMenuName(final String menuName, final Long id) {
        DuplicateCheckRO duplicateCheck = new DuplicateCheckRO();
        duplicateCheck.setTableName(DuplicateCheckConstant.TABLE_NAME_SYS_MENU);
        duplicateCheck.setFieldName(DuplicateCheckConstant.FIELD_NAME_MENU_NAME);
        duplicateCheck.setFieldValue(menuName);
        if (ConvertUtils.isNotEmpty(id)){
            duplicateCheck.setDataId(id);
        }
        return doDuplicateCheck(duplicateCheck);
    }

    /**
     * 组织机构名称重复校验
     *
     * @param organizationName - 组织机构名称
     * @param id - 主键
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/30 12:01
     */
    public Result checkOrganizationName(final String organizationName, final Long id) {
        DuplicateCheckRO duplicateCheck = new DuplicateCheckRO();
        duplicateCheck.setTableName(DuplicateCheckConstant.TABLE_NAME_SYS_ORGANIZATION);
        duplicateCheck.setFieldName(DuplicateCheckConstant.FIELD_NAME_ORGANIZATION_NAME);
        duplicateCheck.setFieldValue(organizationName);
        if (ConvertUtils.isNotEmpty(id)){
            duplicateCheck.setDataId(id);
        }
        return doDuplicateCheck(duplicateCheck);
    }

    /**
     * 定时任务名称重复校验
     *
     * @param quartzJobName - 定时任务名称
     * @param id - 主键
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/30 12:01
     */
    public Result checkQuartzJobName(final String quartzJobName, final Long id) {
        DuplicateCheckRO duplicateCheck = new DuplicateCheckRO();
        duplicateCheck.setTableName(DuplicateCheckConstant.TABLE_NAME_SYS_QUARTZ_JOB);
        duplicateCheck.setFieldName(DuplicateCheckConstant.FIELD_NAME_QUARTZ_JOB_NAME);
        duplicateCheck.setFieldValue(quartzJobName);
        if (ConvertUtils.isNotEmpty(id)){
            duplicateCheck.setDataId(id);
        }
        return doDuplicateCheck(duplicateCheck);
    }

}
